/*
 *  Copyright (C) 2011 JiangHongTiao <jjurco.sk_gmail.com>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove.jianghongtiao.websearch.api.google.search;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author xjuraj
 */
public class GWSResponseDataImpl implements GWSResponseData {

    private JSONObject wsResponseData;
    private GWSCursor gWSCursor;
    private List<GWSResult> gWSResults = new ArrayList<GWSResult>();

    public GWSResponseDataImpl(JSONObject wsResponseData) throws JSONException {
        if (wsResponseData == null) {
            throw new NullPointerException("JSON Object is null.");
        }
        this.wsResponseData = wsResponseData;
        parseWSResponseData(wsResponseData);
    }

    public GWSCursor getCursor() {
        return gWSCursor;
    }

    public List<GWSResult> getResults() {
        return Collections.unmodifiableList(gWSResults);
    }

    public int googleWSCount() {
        return gWSResults.size();
    }

    public GWSResult getResult(int index) {
        return gWSResults.get(index);
    }

    private void parseWSResponseData(JSONObject wsResponseData) throws
            JSONException {
        gWSCursor = new GWSCursorImpl(wsResponseData.getJSONObject("cursor"));
        JSONArray results = wsResponseData.getJSONArray("results");
        for (int i = 0; i < results.length(); i++) {
            JSONObject result = results.getJSONObject(i);
            gWSResults.add(new GWSResultImpl(result, i + 1));
        }
    }

    public boolean appendResults(Collection<GWSResult> results) {
        boolean result = true;
        //append all results and set correct result position
        for (int i = 0; i < results.size(); i++) {
            GWSResult gWSResult = (GWSResult) results.toArray()[i];
            gWSResult.setResultPosition(results.size() + i + 1);
            result = result && gWSResults.add(gWSResult);
        }
        return result;
    }

    public List<URL> getURLResults() {
        List<URL> result = new ArrayList<URL>();
        try {
            for (Iterator<GWSResult> it = gWSResults.iterator(); it.hasNext();) {
                GWSResult searchResult = it.next();
                result.add(new URL(searchResult.getUnescapedUrl()));
            }
        } catch (MalformedURLException ex) {
            Logger.getLogger(GWSResponseDataImpl.class.getName()).
                    log(Level.SEVERE, null, ex);
        }
        return result;
    }
}
